package com.jifeng.sterilizer.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jifeng.sterilizer.entity.ControllerEntity;
import com.jifeng.sterilizer.utils.R;
import com.jifeng.sterilizer.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.sterilizer.utils.PageUtils;
import com.jifeng.sterilizer.utils.Query;

import com.jifeng.sterilizer.dao.ControllerTypeDao;
import com.jifeng.sterilizer.entity.ControllerTypeEntity;
import com.jifeng.sterilizer.service.ControllerTypeService;


@Service("controllerTypeService")
@Slf4j
public class ControllerTypeServiceImpl extends ServiceImpl<ControllerTypeDao, ControllerTypeEntity> implements ControllerTypeService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        try {
            QueryWrapper<ControllerTypeEntity> queryWrapper = new QueryWrapper<>();
            Object name = params.get("name");
            queryWrapper.like("name", name);
            IPage<ControllerTypeEntity> page = this.page(
                    new Query<ControllerTypeEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "查询中控器类型列表", e.getMessage());
            return null;
        }
    }

    @Override
    public R getByName(Map<String, Object> params) {
        try {
            Object name = params.get("name");
            List<ControllerTypeEntity> controllerTypeEntities = this.list(new QueryWrapper<ControllerTypeEntity>().like("name", name));
            if (controllerTypeEntities.size() > 10) {
                controllerTypeEntities.subList(0, 10);
            }
            return R.ok().put("data", controllerTypeEntities);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "根据名称模糊查询中控器类型表", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R del(Map<String, Object> params) {
        try {
            Object id = params.get("id");
            boolean remove = this.remove(new QueryWrapper<ControllerTypeEntity>().eq("id", id));
            if (remove) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "删除中控器类型", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R add(ControllerTypeEntity controllerType) {
        try {
            boolean save = this.save(controllerType);
            if (save) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "新增中控器类型", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R myUpdate(ControllerTypeEntity controllerType) {
        try {
            boolean update = this.updateById(controllerType);
            if (update) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "修改中控器类型", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }
}